﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Av.Utils;
using System.Net;

namespace JrcUtils
{

    internal class IttcConnection
    {
        public string Url { get; set; }
        public string User { get; set; }
        public string Pass { get; set; }
    }

    internal class StatusChangeEventArgs : EventArgs
    {
        public enum OperationType { CstsDownload }
        public enum StatusType { FinishedOk = 1, StartedOk, Error }

        public OperationType Operation { get; set; }
        public StatusType Status { get; set; }
    }

    /// <summary>
    /// Delegate for status change - smth is completed, etc
    /// </summary>
    internal delegate void StatusChangedHandler(object sender, EventArgs e);
    
    internal class IttcCommunicator
    {

        /// <summary>
        /// List of CSTs from ITTC server
        /// </summary>
        protected Dictionary<int, string> m_arCsts = new Dictionary<int, string>();

        /// <summary>
        /// Gets list of CSTs
        /// </summary>
        public Dictionary<int, string> Csts { get { return m_arCsts; } }

        /// <summary>
        /// Indicates that status has changed - completed, error, etc...
        /// </summary>
        public event StatusChangedHandler StatusChanged = null;

        /// <summary>
        /// Fires event to indicate that status was changed
        /// </summary>
        protected void OnStatusChanged(StatusChangeEventArgs.OperationType operation, StatusChangeEventArgs.StatusType status)
        {
            OnStatusChanged(new StatusChangeEventArgs() { Operation = operation, Status = status });
        }

        /// <summary>
        /// Fires event to indicate that status was changed
        /// </summary>
        protected void OnStatusChanged(StatusChangeEventArgs e)
        {
            if( StatusChanged != null )
            {
                StatusChanged("DjangoCommunicator", e);
            }
        }

        public void DownloadCsts(object obj)
        {
            try
            {
                OnStatusChanged(StatusChangeEventArgs.OperationType.CstsDownload, StatusChangeEventArgs.StatusType.StartedOk);

                IttcConnection con = (IttcConnection)obj;
                WebClient wc = new WebClient();
                Log4cs.Log("Getting list of CSTs from {0}...", con.Url);
                string s = wc.DownloadString(con.Url);
                Log4cs.Log("Got from server: {0}", s);

                m_arCsts.Clear();
                string[] ar = s.Split("&".ToCharArray());
                for(int i = 0; i < ar.Length; i++)
                {
                    //Log4cs.Log("Examing: {0}...", ar[i]);
                    string[] param = ar[i].Trim().Split("=".ToCharArray());
                    if( (param != null) && (param.Length == 2) )
                    {
                        Log4cs.Log("{0} => {1}", param[0].Trim(), param[1].Trim());
                        m_arCsts.Add(int.Parse(param[0].Trim()), param[1].Trim().Replace("%20", " "));
                    }
                }

                OnStatusChanged(StatusChangeEventArgs.OperationType.CstsDownload, StatusChangeEventArgs.StatusType.FinishedOk);

            } catch(Exception ex)
            {
                Log4cs.Log(Importance.Error, "Error getting CSTs from server!");
                Log4cs.Log(Importance.Debug, ex.ToString());

                // Fire error event
                OnStatusChanged(StatusChangeEventArgs.OperationType.CstsDownload, StatusChangeEventArgs.StatusType.Error);
            }
        }

    }
}
